import random
import itertools
x = ['+', '-', '*', '/']
def def0():
    '''
    玩家运算，程序判断
    :return:
    '''
    def random_number():
        '''
        随机生成4个数
        :return:
        '''
        y = []
        for i in range(4):
            y.append(random.randint(1, 13))
        return y

    def poker(y):
        """
        将生成的随机数以扑克牌的形式输出
        :param y:
        :return:
        """
        y1 = y.copy()
        while 1 in y1 or 11 in y1 or 12 in y1 or 13 in y1:
            if 1 in y1:
                z = y1.index(1)
                y1.remove(1)
                y1.insert(z, 'A')
            if 11 in y1:
                z = y1.index(11)
                y1.remove(11)
                y1.insert(z, 'J')
            if 12 in y1:
                z = y1.index(12)
                y1.remove(12)
                y1.insert(z, 'Q')
            if 13 in y1:
                z = y1.index(13)
                y1.remove(13)
                y1.insert(z, 'K')
            continue
        print("随机抽取的四张牌为：", y1[0], y1[1], y1[2], y1[3])

    def judge(y):
        '''
        运算
        :param y:
        :return:
        '''
        for i in itertools.permutations(y):
            for j in itertools.product(x, repeat=3):
                a = [("{0}{1}{2}{3}{4}{5}{6}".format(i[0], j[0], i[1], j[1], i[2], j[2], i[3])),
                     ("(({0}{1}{2}){3}{4}){5}{6}".format(i[0], j[0], i[1], j[1], i[2], j[2], i[3])),
                     ("({0}{1}{2}{3}{4}){5}{6}".format(i[0], j[0], i[1], j[1], i[2], j[2], i[3])),
                     ("{0}{1}({2}{3}{4}{5}{6})".format(i[0], j[0], i[1], j[1], i[2], j[2], i[3])),
                     ("{0}{1}({2}{3}({4}{5}{6}))".format(i[0], j[0], i[1], j[1], i[2], j[2], i[3]))]
                for z in a:
                    try:
                        if eval(z) == 24:
                            print(z, "=24")
                    except:
                        pass

    def player():
        '''
        答案判断
        :return:
        '''
        y = random_number()
        poker(y)
        expr = input("请输入算式：")
        if eval(expr) == 24:
            print("答案正确，其他答案为：")
            judge(y)
        else:
            print("答案错误，其他答案为：")
            judge(y)

    player()

def def1():
    '''
    程序运算
    :return:
    '''

    def fun(k, l, i):
        """
        两个数据之间的运算
        :param k:
        :param l:
        :param i:
        :return:
        """
        if i == 0:
            if (type(k) is int or type(k) is float) and (type(l) is int or type(l) is float):
                tem = k + l
                return tem
            else:
                return
        elif i == 1:
            if (type(k) is int or type(k) is float) and (type(l) is int or type(l) is float):
                tem = k - l
                return tem
            else:
                return
        elif i == 2:
            if (type(k) is int or type(k) is float) and (type(l) is int or type(l) is float):
                tem = k * l
                return tem
            else:
                return
        elif i == 3:
            if (type(k) is int or type(k) is float) and (type(l) is int or type(l) is float):
                if l != 0:
                    tem = k / l
                    return tem
                else:
                    return
            else:
                return

    def calculate(y):
        """
        计算，求出符合要求的
        :return:
        """
        result_list = []
        result_list2 = []
        temp = 0
        temp1 = 0
        temp2 = 0
        for i in range(0, 4):
            for j in range(0, 4):
                for k in range(0, 4):
                    for l in range(0, 4):
                        if i == j or i == k or i == l or j == k or j == l or k == l:
                            continue
                        else:
                            for n in range(0, 4):
                                for m in range(0, 4):
                                    for p in range(0, 4):
                                        for q in range(0, 6):
                                            result = 0
                                            if q == 0:
                                                temp1 = fun(y[i], y[j], n)
                                                temp1 = fun(temp1, y[k], m)
                                                result = fun(temp1, y[l], p)
                                                if result == 24:
                                                    result_list.append(
                                                        "(({0}{1}{2}){3}{4}){5}{6}=24".format(y[i], x[n], y[j], x[m],
                                                                                              y[k],
                                                                                              x[p], y[l]))
                                                    result_list2.append(
                                                        "(({0}{1}{2}){3}{4}){5}{6}".format(y[i], x[n], y[j], x[m], y[k],
                                                                                           x[p], y[l]))
                                                    # print("(({0}{1}{2}){3}{4}){5}{6}=24".format(y[i], x[n], y[j], x[m], y[k],
                                                    #                                             x[p], y[l]))
                                                    temp += 1
                                                # break
                                                continue
                                            elif q == 1:
                                                temp1 = fun(y[i], y[j], n)
                                                temp2 = fun(y[k], y[l], p)
                                                result = fun(temp1, temp2, m)
                                                if result == 24:
                                                    result_list.append(
                                                        "({0}{1}{2}){3}({4}{5}{6})=24".format(y[i], x[n], y[j], x[m],
                                                                                              y[k],
                                                                                              x[p], y[l]))
                                                    result_list2.append(
                                                        "({0}{1}{2}){3}({4}{5}{6})".format(y[i], x[n], y[j], x[m], y[k],
                                                                                           x[p], y[l]))
                                                    # print("({0}{1}{2}){3}({4}{5}{6})=24".format(y[i], x[n], y[j], x[m], y[k],
                                                    #                                             x[p], y[l]))
                                                    temp += 1
                                                # break
                                                continue
                                            elif q == 2:
                                                temp1 = fun(y[j], y[k], m)
                                                temp1 = fun(y[i], temp1, n)
                                                result = fun(temp1, y[l], p)
                                                if result == 24:
                                                    result_list.append(
                                                        "({0}{1}({2}{3}{4})){5}{6}=24".format(y[i], x[n], y[j], x[m],
                                                                                              y[k],
                                                                                              x[p], y[l]))
                                                    result_list2.append(
                                                        "({0}{1}({2}{3}{4})){5}{6}".format(y[i], x[n], y[j], x[m], y[k],
                                                                                           x[p], y[l]))
                                                    # print("({0}{1}({2}{3}{4})){5}{6}=24".format(y[i], x[n], y[j], x[m], y[k],
                                                    #                                             x[p], y[l]))
                                                    temp += 1
                                                # break
                                                continue
                                            # elif q==3:
                                            #     temp1 = fun(y[k], y[l], m)
                                            #     temp2 = fun(y[i], y[j], p)
                                            #     result = fun(temp1, temp2, n)
                                            #     if result == 24:
                                            #
                                            #         print("({0}{1}{2}){3}({4}{5}{6})=24".format(y[i], x[n], y[j], x[m], y[k],
                                            #                                                     x[p], y[l]))
                                            #         temp += 1
                                            #         break
                                            elif q == 3:
                                                temp1 = fun(y[j], y[k], m)
                                                temp1 = fun(temp1, y[l], p)
                                                result = fun(y[i], temp1, n)
                                                if result == 24:
                                                    result_list.append(
                                                        "{0}{1}(({2}{3}{4}){5}{6})=24".format(y[i], x[n], y[j], x[m],
                                                                                              y[k],
                                                                                              x[p], y[l]))
                                                    result_list2.append(
                                                        "{0}{1}(({2}{3}{4}){5}{6})".format(y[i], x[n], y[j], x[m], y[k],
                                                                                           x[p], y[l]))
                                                    # print("{0}{1}(({2}{3}{4}){5}{6})=24".format(y[i], x[n], y[j], x[m], y[k],
                                                    #                                             x[p], y[l]))
                                                    temp += 1
                                                # break
                                                continue
                                            elif q == 4:
                                                temp1 = fun(y[k], y[l], p)
                                                temp1 = fun(y[j], temp1, m)
                                                result = fun(y[i], temp1, n)
                                                if result == 24:
                                                    result_list.append(
                                                        "{0}{1}({2}{3}({4}{5}{6}))=24".format(y[i], x[n], y[j], x[m],
                                                                                              y[k],
                                                                                              x[p], y[l]))
                                                    result_list2.append(
                                                        "{0}{1}({2}{3}({4}{5}{6}))".format(y[i], x[n], y[j], x[m], y[k],
                                                                                           x[p], y[l]))
                                                    # print("{0}{1}({2}{3}({4}{5}{6}))=24".format(y[i], x[n], y[j], x[m], y[k],
                                                    #                                             x[p], y[l]))
                                                    temp += 1
                                                # break
                                                continue
        return temp, result_list, result_list2
        # if temp == 0:
        #     print("这四张牌无法完成24点游戏")
        # else:
        #     print()

    def player():
        '''
        玩家输入
        :return:
        '''
        a = int(input("请输入第一个数:"))
        b = int(input("请输入第二个数:"))
        c = int(input("请输入第三个数:"))
        d = int(input("请输入第四个数:"))
        y = [a, b, c, d]
        result, result_list, result_list2 = calculate(y)
        print("这四张牌所有的组合为：")
        for i in result_list:
            print("{0}".format(i))

    player()

def main():
    print("===================================================")
    print("==                     24点游戏                   ==")
    print("==程序出题玩家计算请输入1:                           ==")
    print("==玩家出题程序计算请输入2:                           ==")
    print("==退出程序请输入3：                                 ==")
    print("===================================================")
    a = int(input("请选择模式（输入1 or 2 or 3）"))
    while a != 3:
        if a == 1:
            def0()
        elif a == 2:
            def1()
        a = int(input("请选择模式（输入1 or 2 or 3）"))

main()